package com.chenfan.mcn.enums;

import com.chenfan.common.extension.dict.DictTranslator;
import lombok.Getter;

import java.util.Objects;

/**
 * 红人返点合同状态枚举
 *
 * @author wulg
 * @date 2021-11-16
 **/
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
public enum StarRebateStatusEnum implements DictTranslator<Integer, String> {

    UN_COMMIT(0, "待提交"),
    APPROVAL(1, "审批中"),
    UN_PAYBACK(2, "待回款"),
    REJECTED(3, "审批拒绝"),
    CANCEL(4, "已撤回"),
    PART_PAYBACK(5, "部分回款"),
    ALL_PAYBACK(6, "已回款"),
    INVALID(9, "已作废");

    @Getter
    private final int code;

    @Getter
    private final String name;

    StarRebateStatusEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    /**
     * 根据code获取 枚举
     * @param code
     * @return
     */
    public static StarRebateStatusEnum toEnum(Integer code) {
        if(Objects.isNull(code)) {
            return null;
        }
        StarRebateStatusEnum starRebateStatusEnum = null;
        for (StarRebateStatusEnum item : StarRebateStatusEnum.values()) {
            if (item.getCode() == code.intValue()) {
                starRebateStatusEnum = item;
                break;
            }
        }
        return starRebateStatusEnum;
    }

    /**
     * 获取状态名称
     * @param code
     * @return
     */
    public static String getStatusName(Integer code) {
        StarRebateStatusEnum starRebateStatusEnum = StarRebateStatusEnum.toEnum(code);
        return starRebateStatusEnum != null ? starRebateStatusEnum.name : null;
    }

    /**
     * 是否待提交
     * @param approvalStatus
     * @return
     */
    public static boolean isUncommit(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.UN_COMMIT.code;
    }

    /**
     * 是否审批中
     * @param approvalStatus
     * @return
     */
    public static boolean isApproval(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.APPROVAL.code;
    }

    /**
     * 是否待回款
     * @param approvalStatus
     * @return
     */
    public static boolean unPackback(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.UN_PAYBACK.code;
    }


    /**
     * 是否作废
     * @param approvalStatus
     * @return
     */
    public static boolean isInvalid(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.INVALID.code;
    }

    /**
     * 是否撤回
     * @param approvalStatus
     * @return
     */
    public static boolean isCancel(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.CANCEL.code;
    }


    /**
     * 是否全部回款
     * @param approvalStatus
     * @return
     */
    public static boolean isAllPayback(Integer approvalStatus) {
        return Objects.nonNull(approvalStatus) && approvalStatus.intValue() == StarRebateStatusEnum.ALL_PAYBACK.code;
    }

    /** 根据状态码任意匹配
     * @param code
     * @param items
     * @return
     */
    public static boolean anyMatch(Integer code, StarRebateStatusEnum... items) {
        boolean match = false;
        if(code != null) {
            for(StarRebateStatusEnum item : items) {
                if(item.getCode() == code.intValue()) {
                    match = true;
                    break;
                }
            }
        }
        return match;
    }

    @Override
    public String translate(Integer code) {
        return Objects.nonNull(code) ? getStatusName(code.intValue()) : null;
    }
}
